package summer.util;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import summer.Message;

public class ChatWindow2 extends JFrame {

    private static final long serialVersionUID = 1L;

    private static final int CON_LEFT = 3;
    private static final int CON_TOP = 22;
    private static final int CON_RIGHT = 3;
    private static final int CON_BOTTOM = 3;
    private static final int CON_PADDING_LEFT = 2;
    private static final int CON_PADDING_TOP = 2;
    private static final int CON_PADDING_RIGHT = 1;
    private static final int CON_PADDING_BOTTOM = 2;
    private static final int PADDING = 6;

    // 内容区宽度
    private static final int CON_WIDTH = 400;
    // 内容区上部高度
    private static final int CON_TOP_HEIGHT = 500;
    // 内容区下部高度
    private static final int CON_BOTTOM_HEIGHT = 60;
    // 窗口初始位置
    private static final int WIN_START_X = 100;
    private static final int WIN_START_Y = 100;
    // 窗口宽
    private static final int WIN_WIDTH = CON_LEFT + CON_PADDING_LEFT + CON_WIDTH + CON_PADDING_RIGHT + CON_RIGHT;
    // 窗口高
    private static final int WIN_HEIGHT = CON_TOP + CON_PADDING_TOP + CON_TOP_HEIGHT + PADDING +
            CON_BOTTOM_HEIGHT + CON_PADDING_BOTTOM + CON_BOTTOM;

    private JButton messageSend;
    private JTextArea messageEditor;
    private JPanel centerScrollPanel;

    public void receive(String receiver, String owner, long time, String content) {
    }

    public ChatWindow2() {
        // Say Hello
        JFrame frame = new JFrame(" Say Hello");
        frame.setLayout(null);
        frame.setBounds(WIN_START_X, WIN_START_Y, WIN_WIDTH, WIN_HEIGHT);
        frame.setVisible(false);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 上半部分
        // 上班部分框架
        centerScrollPanel = new JPanel();
        BoxLayout layout=new BoxLayout(centerScrollPanel, BoxLayout.Y_AXIS);
        centerScrollPanel.setLayout(layout);

        for(int i = 0; i < 100; i++) {
            Message mess = new Message("jiang", "gu", i * 10000, "如前所述，BoxLayout可以把控件依次进行水平或者垂直排列布局，这是通过参数 X_AXIS、Y_AXIS 来决定的。X_AXIS 表示水平排列，而 Y_AXIS 表示垂直排列。BoxLayout 的构造函数有两个参数，一个参数定义使用该 BoxLayout 的容器，另一个参数是指定 BoxLayout 是采用水平还是垂直排列。下面是一个创建 BoxLayout 实例的例子");
            centerScrollPanel.add(mess);
        }
        JScrollPane centerScroll = new JScrollPane(centerScrollPanel);
        centerScroll.setBounds(CON_PADDING_LEFT, CON_PADDING_TOP, CON_WIDTH, CON_TOP_HEIGHT);
        centerScroll.setBorder(BorderFactory.createEmptyBorder());
        centerScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        centerScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        frame.add(centerScroll);

        // 下半部分
        messageSend = new JButton("发送");
        messageSend.setBounds(
                CON_PADDING_LEFT + CON_WIDTH - 60 ,
                CON_PADDING_TOP + CON_TOP_HEIGHT + PADDING,
                60,
                CON_BOTTOM_HEIGHT);
        frame.add(messageSend);

        messageEditor = new JTextArea();
        messageEditor.setLineWrap(true);
        JScrollPane messScroll = new JScrollPane(messageEditor);
        messScroll.setBounds(
                CON_PADDING_LEFT,
                CON_PADDING_TOP + CON_TOP_HEIGHT + PADDING,
                CON_WIDTH - PADDING - 60,
                CON_BOTTOM_HEIGHT + 1);
        messScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        messScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        frame.add(messScroll);
        frame.setVisible(true);
    }



    public static void main(String[] args) throws Exception {
        new ChatWindow2();
    }
}